package com.gexin.model.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.utils.StringUtil;
import com.jfinal.log.Log;

/**
 * 权限工具类
 * 
 * @author 肖得明
 */
public class RoleKit {
	static final Log log = Log.getLog(RoleKit.class);
	// 存储系统后台所有菜单的map对象
	public static Map<String, String[]> ResMap = new HashMap<String, String[]>();
	//用户ID -  用户资源菜单的map
	public static Map<Integer, Map<String, String>> RolMap = new HashMap<Integer, Map<String, String>>();
	
	//用户ID -  对应权限组代码
	public static Map<Integer, Map<String, String>> UserRolCode = new HashMap<Integer, Map<String, String>>();
	
	//用户ID -  对应权限组ID
	public static Map<Integer, Map<String, String>> UserRolId = new HashMap<Integer, Map<String, String>>();
	
	// 系统菜单的缓存
	private final static String Cache_Key_Role = "Admin_Role_Key";
	
	public static Map<String, String> getRolMap(Integer  user_id){
		if(!RolMap.containsKey(user_id)){
			getRoleResByUserId(user_id);
		}
		return  RolMap.get(user_id);
	}
	
	public static Map<String, String> getUserRolId(Integer  user_id){
		if(!UserRolId.containsKey(user_id)){
			getRoleResByUserId(user_id);
		}
		return  UserRolId.get(user_id);
	}
	
	
	public static Map<String, String> getUserRolCode(Integer  user_id){
		if(!UserRolCode.containsKey(user_id)){
			getRoleResByUserId(user_id);
		}
		return  UserRolCode.get(user_id);
	}
	
	// 初始化全局的菜单
	public static void init() {
		log.info("开始初始化权限工具类型.......");// where res.pid is not null
		String sql = "select  res.id,res.url,res.css_tag,(select css_tag FROM  t_admin_res where res.css_pid= id ) as pcss_tag   from t_admin_res as res";
		List<AdminRes> list = AdminRes.dao.find(sql);
		for (AdminRes res : list) {
			// System.err.println(res.getUrl());
			String css_tag = null;
			if (StringUtil.isBlank(res.getCssTag())) {
				css_tag = res.getStr("pcss_tag");
			} else {
				css_tag = res.getCssTag();
			}
			ResMap.put(res.getUrl(), (res.getStr("pcss_tag") + "-" + css_tag).split("-"));
		}

		log.info("初始化权限完毕.........");
	}

	/**
	 * 获取权限菜单的CSS 和焦点功能
	 * 
	 * @param key
	 * @return
	 */
	public static String[] getResMap(String key) {
		String[] sp = ResMap.get(key);
		if (sp == null) {
			sp = new String[] { "home", "index" };
		}
		return sp;

	}

	/**
	 * 检测权限
	 * @param roleid 权限组ID
	 * @param url  地址
	 * @return
	 */
	public static boolean checkRole(Integer user_id, String url) {
		if(!RolMap.containsKey(user_id)){
			getRoleResByUserId(user_id);
		}
		return RolMap.get(user_id).containsKey(url) ? true : false;
	}

	/**
	 * 从缓存 读取对应
	 * @param roleid 权限组的ID
	 * @return
	 */
	public static Map<AdminRes, List<AdminRes>> getRoleResByUserId(Integer userId) {
		Map<AdminRes, List<AdminRes>> map = CacheUtils.getEhcache(Cache_Key_Role + userId);
		if (map == null) {
			map = AdminRes.dao.getResByUserId(userId);
			//RolMap.put(userId, AdminRes.dao.findResUrlByUserId(userId));
			CacheUtils.setEhcache(Cache_Key_Role + userId, map);
		}
		RolMap.put(userId, AdminRes.dao.findResUrlByUserId(userId));
		UserRolId.put(userId, AdminUserRole.dao.getUserRole(userId));
		UserRolCode.put(userId, AdminUserRole.dao.getUserRoleCode(userId));
		return map;
	}

	/**
	 * 删除权限的缓存
	 * 
	 * @param roleid
	 */
	public static void clearUserRoleCache(Integer userId) {
		log.info("清理用户权限缓存...  用户ID：" + userId);
		CacheUtils.delEhcache(Cache_Key_Role + userId);
		RolMap.remove(userId);
		log.info("重新载入 缓存到map  里面去");
		RolMap.put(userId, AdminRes.dao.findResUrlByUserId(userId));
		UserRolCode.put(userId, AdminUserRole.dao.getUserRoleCode(userId));
		UserRolId.put(userId, AdminUserRole.dao.getUserRole(userId));
	}

	/**
	 * 清除所有的缓存
	 */
	public static void clear_Role_All() {
		List<AdminUser> user_list = AdminUser.dao.find("select * from t_admin_user");
		for (AdminUser user : user_list) {
			clearUserRoleCache(user.getId());
		}
	}
}
